﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.Utils;
using System.IO;
using CMS.BL.Entity;
using System.Globalization;

namespace CMS.UI
{
    public class MailingService
    {
        /// <summary>
        /// MailingService variables (email templates names)
        /// </summary>
        public enum EmailVariables
        {
            ForgottenPassword,
            ContactForm,
            NewComment,
            WelcomeMessage
        }

        private static string GetTemplate(EmailVariables template, Dictionary<string, object> data, CultureInfo culture = null, string language = "")
        {
            string templateName = template.ToString();
            if (!String.IsNullOrEmpty(language))
                templateName += language;

            var templatePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(SettingsManager.Email.Templates.Path), templateName.ToString() + SettingsManager.Email.Templates.Type);

            return TemplateEngine.ProcessTemplate(templatePath, data, culture);
        }

        public static void SendForgottenPassword(string userName, string pass, string email, string domain, string path)
        {
            var templateData = new Dictionary<string, object>();
            templateData.Add("USER", userName);
            templateData.Add("PASS", pass);
            templateData.Add("DOMAIN", domain);
            templateData.Add("PATH", path);

            var template = GetTemplate(EmailVariables.ForgottenPassword, templateData);

            var recipients = new List<string>() { email };
            MailSender.Instance.SendEmail(recipients, null, "Forgotten password", template);
        }

        public static void SendWelcomeMessage(string userName, string email, string company, string domain, string comPhone)
        {
            var templateData = new Dictionary<string, object>();
            templateData.Add("FULLUSERNAME", userName);
            templateData.Add("COMPANY", company);
            templateData.Add("USEREMAIL", email);
            templateData.Add("PHONE", comPhone);
            templateData.Add("DOMAIN", domain);
            var template = GetTemplate(EmailVariables.WelcomeMessage, templateData);
            var recipients = new List<string>() { email };
            MailSender.Instance.SendEmail(recipients, null, String.Format("Welcome to {0}", domain), template);
        }

        public static void SendCustomForm(List<KeyValuePair<string, string>> items, string recipients)
        {
            if (!string.IsNullOrEmpty(recipients))
            {
                var templateData = new Dictionary<string, object>();
                templateData.Add("FORMITEMS", items);

                var template = GetTemplate(EmailVariables.ContactForm, templateData);

                MailSender.Instance.SendEmail(recipients.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>(), null, "Contact Us", template);
            }
        }

        public static void SendNewComment(string postlink, string adminlink, string companyName, string domain, string recipient)
        {
            if (!string.IsNullOrEmpty(recipient))
            {
                var templateData = new Dictionary<string, object>();
                templateData.Add("POSTLINK", postlink);
                templateData.Add("ADMINLINK", adminlink);
                templateData.Add("COMPANYNAME", companyName);
                templateData.Add("DOMAIN", domain);

                var template = GetTemplate(EmailVariables.NewComment, templateData);

                var recipients = new List<string>() { recipient };
                MailSender.Instance.SendEmail(recipients, null, "New Comment", template);
            }
        }
    }
}